package com.longshen.dubbo.api.restful;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.longshen.dubbo.api.anno.LoginRequired;
import com.longshen.dubbo.api.anno.TradeRequired;
import com.longshen.dubbo.api.base.ErrorCode;
import com.longshen.dubbo.api.base.RestResult;
import com.longshen.dubbo.api.model.AdminUser;
import com.longshen.dubbo.api.model.AdminUserRole;
import com.longshen.dubbo.api.restful.vo.ModeId;
import com.longshen.dubbo.api.restful.vo.UserListIn;
import com.longshen.dubbo.api.restful.vo.UserListOut;
import com.longshen.dubbo.api.restful.vo.UserVO;
import com.longshen.dubbo.api.restful.vo.UserVOIn;
import com.longshen.dubbo.api.service.AdminUserRoleService;
import com.longshen.dubbo.api.service.AdminUserService;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @ClassName: AdminUserController
 * @Description:用户服务控制器
 * @author: longshen
 * @date: 2019年10月17日 上午9:59:10
 */
@Slf4j
@RequestMapping(value = "/user")
@RestController
@LoginRequired
public class AdminUserController {

	@Autowired
	private AdminUserService adminUserService;
	@Autowired
	private AdminUserRoleService adminUserRoleService;

	@RequestMapping(value = "/userinfo", method = RequestMethod.POST)
	public RestResult<UserVO> userInfo(@RequestBody ModeId in) {
		AdminUser user = adminUserService.findById(in.getId());
		if (user != null) {
			UserVO vo = new UserVO();
			BeanUtils.copyProperties(user, vo);
			RestResult rest = RestResult.success();
			rest.setData(vo);
			return rest;
		} else {
			RestResult rest = RestResult.newInstance();
			rest.setErrorCode(ErrorCode.E_000003);
			return rest;
		}

	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	public RestResult<UserListOut> list(@RequestBody UserListIn in) {
		Page<AdminUser> pages = adminUserService.pageList(in.getPagesize(), in.getPagenum(), in.getUsername());
		if (pages != null && pages.getPageSize() > 0) {
			UserListOut out = new UserListOut();
			List<UserVO> list = new ArrayList<UserVO>();
			for (AdminUser adminUser : pages) {
				UserVO vo = new UserVO();
				BeanUtils.copyProperties(adminUser, vo);
				list.add(vo);
			}
			out.setCurrentsize(pages.getPageSize());
			out.setTotalsize((int) pages.getTotal());
			out.setList(list);
			RestResult rest = RestResult.success();
			rest.setData(out);
			return rest;
		} else {
			RestResult rest = RestResult.newInstance();
			rest.setErrorCode(ErrorCode.E_000003);
			return rest;
		}

	}

	@TradeRequired(tradeCode = "USER_ADD")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public RestResult add(@RequestBody UserVOIn in) {
		AdminUser user = new AdminUser();
		BeanUtils.copyProperties(in, user);
		try {

			adminUserService.add(user, in.getRoleid());
			return RestResult.success();
		} catch (DuplicateKeyException e) {
			log.error("添加用户异常", e);
			RestResult rest = RestResult.newInstance();
			rest.setErrorCode(ErrorCode.E_000005);
			rest.setMessage(rest.getMessage() + in.getCellphone());
			return rest;
		} catch (Exception e) {
			return RestResult.fail(ErrorCode.E_999999);
		}

	}

	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public RestResult del(@RequestBody ModeId in) {
		try {

			adminUserService.delById(in.getId());
			return RestResult.success();
		} catch (Exception e) {
			return RestResult.fail(ErrorCode.E_999999);
		}

	}

	@RequestMapping(value = "/mod", method = RequestMethod.POST)
	public RestResult mod(@RequestBody UserVOIn in) {
		AdminUser user = new AdminUser();
		BeanUtils.copyProperties(in, user);
		try {

			adminUserService.mod(user, in.getRoleid());
			return RestResult.success();
		} catch (DuplicateKeyException e) {
			log.error("修改用户异常", e);
			RestResult rest = RestResult.newInstance();
			rest.setErrorCode(ErrorCode.E_000005);
			rest.setMessage(rest.getMessage() + in.getCellphone());
			return rest;
		} catch (Exception e) {
			return RestResult.fail(ErrorCode.E_999999);
		}

	}

	@RequestMapping(value = "/queryid", method = RequestMethod.POST)
	public RestResult queryById(@RequestBody ModeId in) {

		AdminUser au = adminUserService.findById(in.getId());
		AdminUserRole aru = adminUserRoleService.findByUserId(in.getId());

		UserVO vo = new UserVO();
		if (au != null)
			BeanUtils.copyProperties(au, vo);
		if (aru != null) {
			vo.setRoleid(aru.getRoleId());
		}

		return RestResult.success(vo);

	}

}
